﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.UI;
using JiravaNet.Web.UI.Fieldset;

namespace JiravaNet.Web.UI.Fieldset {
	/// <summary>
	/// Extenzni trida pro datovy formular
	/// </summary>
	public static class FieldsetExtensions {

		/// <summary>
		/// Zajisti vytvoreni instance tridy datoveho formulare a jeho vykresleni
		/// </summary>
		/// <param name="helper"><see cref="HtmlHelper"/> trida</param>
		/// <param name="caption">nadpisek polozek</param>
		/// <param name="items">definice polozek</param>
		public static void Fieldset(this HtmlHelper helper, string caption, Action<IFieldsetBuilder> items) {
			var dataForm = new Fieldset(caption, createDataFormItemBuilder(items),
					helper.ViewContext.HttpContext.Response.Output, helper);
			dataForm.Render();
		}

		/// <summary>
		/// Vytvoreni builderu, ktery vypise polozky z jejich definice
		/// </summary>
		/// <param name="items">definice polozek</param>
		/// <returns>instance builderu</returns>
		private static FieldsetBuilder createDataFormItemBuilder(Action<IFieldsetBuilder> items){
			var builder = new FieldsetBuilder();
			if (items != null) {
				items(builder);
			}
			return builder;
		}
	}
}